﻿using System;
using System.Windows.Forms;

namespace Oxage.Forms
{
	public partial class MainForm : Form
	{
		#region Members
		private AboutForm aboutForm;
		private MonitorForm monitorForm;
		private RedirectForm redirectForm;
		#endregion

		#region Constructor
		public MainForm()
		{
			InitializeComponent();
			tray.BalloonTipTitle = Application.ProductName;
		}
		#endregion

		#region Methods
		protected void ShowRedirectForm()
		{
			//Try to use existing form - only one form can be displayed at the time
			if (this.redirectForm == null || this.redirectForm.IsDisposed)
			{
				this.redirectForm = new RedirectForm();
				this.redirectForm.ReportMessage += (sender, e) =>
				{
					if (this.monitorForm != null)
					{
						this.monitorForm.AppendLine(e.Result);
					}
				};
			}
			this.redirectForm.Show();
		}

		protected void ShowMonitorForm()
		{
			if (this.monitorForm == null || this.monitorForm.IsDisposed)
			{
				this.monitorForm = new MonitorForm();
			}
			this.monitorForm.Clear();
			this.monitorForm.Show();
		}
		#endregion

		#region Event handlers
		private void MainForm_Load(object sender, EventArgs e)
		{
			//Show welcome balloon
			if (AppConfig.IsFirstRun)
			{
				tray.ShowBalloonTip(AppConfig.BalloonTimeout, "Welcome!", "Double click to open the window.", ToolTipIcon.Info);
			}
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			tray.Visible = false;
		}

		private void popup_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			if (e.ClickedItem == itemRedirect)
			{
				ShowRedirectForm();
			}
			else if (e.ClickedItem == itemMonitor)
			{
				ShowMonitorForm();
			}
			else if (e.ClickedItem == itemAbout)
			{
				aboutForm = new AboutForm();
				aboutForm.Show();
			}
			else if (e.ClickedItem == itemExit)
			{
				Application.Exit();
			}
		}

		private void tray_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			ShowRedirectForm();
		}
		#endregion
	}
}